// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        rope_node: {
            default: null,
            type: cc.Node
        },
        cow_ins: {
            default: null,
            type: cc.Node
        },
        rope_imgs: {
            default: [],
            type: cc.SpriteFrame
        },
        cow_prefab:{
            default:null,
            type:cc.Prefab
        },
        time:{
            default: 0
        }
    },

    // LIFE-CYCLE CALLBACKS:

     onLoad () {
        this.success = false;
         this.scoreNum = 0;

     },

    start() {
        let lblCountDown = cc.find("Canvas/bg_sprite/countDown").getComponent(cc.Label)
        // let time = 60;
        // 倒计时
        lblCountDown.string = "Time: " + this.time + " s";
        this.schedule(function () {
            this.time--;
            lblCountDown.string = "Time: " + this.time + " s";
            if (this.time == 0) {
                cc.log("游戏结束");
                let resultNode = cc.find("Canvas/result");
                resultNode.active = true

                let lblTitle = resultNode.getChildByName("title").getComponent(cc.Label);
                lblTitle.string = "最终得分 " + this.scoreNum;
                let lblContent = resultNode.getChildByName("content").getComponent(cc.Label);
                switch (true) {
                    case this.scoreNum <= 2:
                        lblContent.string = "你菜鸡";
                        break;

                    case this.scoreNum <= 4:
                        lblContent.string = "你高手";
                        break;

                    case this.scoreNum > 6:
                        lblContent.string = "你牛逼";
                        break;

                }
                //游戏结束，暂停游戏
                cc.director.pause();

            }
        },1);
    },

    clickCapture: function (event, customEventDate) {
        cc.log("clickCapture")
        // 激活当前节点
        this.rope_node.active = true
        // siblingIndex是设置节点在父节点中的排序
        this.rope_node.setSiblingIndex(100);
        // 设置当前位置
        this.rope_node.y = -480;
        // 开始动作
        const up = cc.moveTo(0.5, this.rope_node.x, 0);
        // 捕捉结果判定
        let result = cc.callFunc(function () {

            const cow_currentX = this.cow_ins.x;

            if (cow_currentX > -50 & cow_currentX < 50) {
                console.log("捕捉成功")
                let bgNode = this.node.getChildByName("bg_sprite");
                bgNode.removeChild(this.cow_ins)
                // 更换绳子
                let ropeType = this.cow_ins.getComponent("cow").randomType+1;
                this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_imgs[ropeType];
                // 生成新的牛
                this.cow_ins = cc.instantiate(this.cow_prefab);
                bgNode.addChild(this.cow_ins)
                this.success = true;
                this.scoreNum++;
            }
        }, this);
        const down = cc.moveTo(0.5, this.rope_node.x, -600);
        let finish = cc.callFunc(function () {
            // 换绳子
            this.rope_node.getComponent(cc.Sprite).spriteFrame = this.rope_imgs[0]
            if (this.success) {
                let lblScore = cc.find("Canvas/bg_sprite/lbl_score").getComponent(cc.Label);
                lblScore.string = "Score:" + this.scoreNum;
                this.success = false;
            }
        }, this);

        let capture_action = cc.sequence(up, result, down, finish);
        this.rope_node.runAction(capture_action)


    },
    closeBtn(){
        cc.log("closeBtn")
        //恢复被暂停的游戏
        cc.director.resume();
        cc.director.loadScene("game");
    }

    // update (dt) {},
});
